// pages/zixun_detail/private_message/private_message.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consultant:{},
    content:"",
    count:0,
    privateMessage:[],
  },
  returnhuifu(){
    wx.navigateBack({
      delta:1
    })
  },
  fasong(){
    if(this.data.content == ""){
      return
    }
    const user = wx.getStorageSync('user')

    wx.request({
      url: 'http://120.26.112.194:80/api/privateMessage',
      method: 'POST',
      data:{
        sixinContent:this.data.content,
        userId:user.userId,
        consultantId:this.data.consultant.consultantId
      },
      success:()=>{
        wx.showToast({
          title: '发送成功',
          duration: 1000,
        })
        this.setData({
          content:""
        })
        this.getData()
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: 'http://120.26.112.194:80/api/consultants/selectByConsultantId?consultantId=' +options.consultantId ,
      method: 'GET',
      success:(res)=>{
        console.log(res.data.data)
        this.setData({
          consultant:res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getData()
  },
  changeTimeFormat(data) {
    return data.map(item => ({
      ...item,
      questionTime: this.formatDate(item.questionTime),
      replyTime: item.replyTime ? this.formatDate(item.replyTime) : null
    }));
  },

  formatDate(isoString) {
    const date = new Date(isoString);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，所以需要加1
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0'); // 获取小时
    const minutes = String(date.getMinutes()).padStart(2, '0'); // 获取分钟
    const seconds = String(date.getSeconds()).padStart(2, '0'); // 获取秒

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },

  getData(){
    const user = wx.getStorageSync('user')
    wx.request({
      url: 'http://120.26.112.194:80/api/privateMessage?userId=' + user.userId,
      method: 'GET',
      success:(res)=>{
        console.log(res.data.data)
        const newData = res.data.data
        this.setData({
          privateMessage:this.changeTimeFormat(newData),
          count:3-res.data.data.length
        })
      }

    })
  },
  onContentChange: function (e) {
    const newContent = e.detail.value;
    this.setData({
      content: newContent  // 更新 data.content
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})